package com.example.netlibrary

import com.example.baselibrary.base.BaseApplication
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.internal.Internal.instance
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import java.util.concurrent.TimeUnit

object RetrofitClient {

    const val BASE_URL = "https://wanandroid.com/"

    private var retrofit: Retrofit? = null

    val server: ApiServer by lazy {
        getRetrofit().create(ApiServer::class.java)
    }

    private fun getRetrofit(): Retrofit {
        if (retrofit == null) {
            retrofit = Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(getOkHttpClent())
                .addConverterFactory(GsonConverterFactory.create())
//                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build()
        }
        return retrofit!!
    }

    private fun getOkHttpClent(): OkHttpClient {
        val builder = OkHttpClient().newBuilder()

        val cacheFile = File(BaseApplication.cacheDir, "cache")
        val cache = Cache(cacheFile, 1024 * 1024 * 50)// 50M 的缓存大小

        builder.run {
            cache(cache)
            connectTimeout(60, TimeUnit.SECONDS)
            readTimeout(60, TimeUnit.SECONDS)
            writeTimeout(60, TimeUnit.SECONDS)
            retryOnConnectionFailure(true)//错误重连
        }

        return builder.build()
    }



}